﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using ADOX;

namespace iHub
{
    public partial class documentselect : Form
    {
        public static documentselect form = null;
        public static string tablename;
        public documentselect()
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Form_Closing);
        }

        private void Form_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                main.form.Enabled = true;
            }
        }

        private void importdoc_Click(object sender, EventArgs e)
        {
            string path;
            Boolean type = false;
            List<string> tables = new List<string>();

            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Microsoft Excel Document (*.xls, *.xlsx)|*.xls;*.xlsx|All Files (*.*)|*.*";
            open.FilterIndex = 1;
            DialogResult result = open.ShowDialog();

            if (result == DialogResult.OK)
            {
                if (reimbursement.Checked)
                {
                    type = true;
                }
                else if (rcbc.Checked)
                {
                    type = false;
                }

                path = open.FileName;
                tables = GetTableExcel(path);
                selecttable select = new selecttable(tables);
                select.ShowDialog(this);
                select.Dispose();
                if ((tablename != string.Empty) && (tablename != null))
                {
                    form = this;
                    importedfile import = new importedfile(path, type, tablename);
                    import.Show();
                    this.Enabled = false;
                }
            }
        }

        public static List<string> GetTableExcel(string strFileName)
        {
            List<string> strTables = new List<string>();
            try
            {
                Catalog oCatlog = new Catalog();
                ADOX.Table oTable = new ADOX.Table();
                ADODB.Connection oConn = new ADODB.Connection();
                oConn.Open("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + strFileName + "; Extended Properties = \"Excel 8.0;HDR=Yes;IMEX=1\";", "", "", 0);
                oCatlog.ActiveConnection = oConn;
                if (oCatlog.Tables.Count > 0)
                {
                    foreach (ADOX.Table tab in oCatlog.Tables)
                    {
                        if (tab.Type == "TABLE")
                        {
                            strTables.Add(tab.Name);
                        }
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            return strTables;
        }
    }
}
